<?php

class Card
{
    public $id;
    public $title;
    public $body;
    public $full_name;
    public $id_number;
    public $birthday;
    public $phone_number;
    public $email;
    public $card_number;
    public $money;
    public $card_name;

    public static function getElementById($nid)
    {
        $node = node_load($nid);
        if($node !== FALSE) return self::toModel($node);
        return NULL;
    }

    public static function getElementByCardNumber($cardNumber = ''){
        $query = new EntityFieldQuery();
        $entity = $query->entityCondition('entity_type','node')
            ->propertyCondition('type','card')
            ->fieldCondition('field_card_number','value',$cardNumber)
            ->execute();

        $result = array();

        if(isset($entity['node'])){

            foreach($entity['node'] as $key => $value){
                $result[] = self::getElementById($key);
            }
        }

        return (count($result) > 0) ? $result[0] : null;
    }

    public static function toModel($node)
    {
        $model = new Card();
        $model->id = $node->nid;
        $model->title = $node->title;

        $temp = field_get_items('node', $node, 'body');
        $model->body = $temp[0]['value'];

        $temp = field_get_items('node', $node, 'field_full_name');
        if(count($temp) > 0) $model->full_name = $temp[0]['value'];

        $temp = field_get_items('node', $node, 'field_id_number');
        if(count($temp) > 0) $model->id_number = $temp[0]['value'];

        $temp = field_get_items('node', $node, 'field_birthday');
        if(count($temp) > 0) $model->birthday = $temp[0]['value'];

        $temp = field_get_items('node', $node, 'field_phone_number');
        if(count($temp) > 0) $model->phone_number = $temp[0]['value'];

        $temp = field_get_items('node', $node, 'field_email');
        if(count($temp) > 0) $model->email = $temp[0]['value'];

        $temp = field_get_items('node', $node, 'field_card_number');
        if(count($temp) > 0) $model->card_number = $temp[0]['value'];

        $temp = field_get_items('node', $node, 'field_money');
        if(count($temp) > 0) $model->money = $temp[0]['value'];

        $temp = field_get_items('node', $node, 'field_card_name');
        if(count($temp) > 0) $model->card_name = $temp[0]['value'];

        return $model;
    }


    public function save()
    {
        if($this->id == '')
        {
            global $user;

            // Create node with default values.
            $entity = entity_create('node', array(
                'type' => 'card',
                'uid' => $user->uid,
                'status' => 1,
                'comment' => 1,
                'promote' => 0,
                'created' => time(),
                'language' => LANGUAGE_NONE
            ));
        }
        else
        {
            $entity = node_load($this->id);

        }

        // Use a wrapper for easy getter and setter.
        $wrapper = entity_metadata_wrapper('node', $entity);
        $wrapper->title->set($this->title);
        $entity->body[LANGUAGE_NONE][0] = array(
            'value' => $this->body,
            'format' => 'full_html',
        );

        $wrapper->field_full_name->set($this->full_name);
        $wrapper->field_id_number->set($this->id_number);
        $wrapper->field_birthday->set($this->birthday);
        $wrapper->field_phone_number->set($this->phone_number);
        $wrapper->field_email->set($this->email);
        $wrapper->field_card_number->set($this->card_number);
        $wrapper->field_money->set($this->money);
        $wrapper->field_card_name->set($this->card_name);

        // Save all values to node.
        $wrapper->save(TRUE);

        // Save node to db.
        entity_save('node', $entity);

    }

}

?>